// "h5":{
// 	"devServer": {
// 		"port": 8080,
// 		"disableHostCheck": true,
// 		"proxy": {
// 			"/api": {
// 				"target": "http://192.168.101.60:8080/",
// 				"changeOrigin": true,
// 				"secure": false,
//				"pathRewrite": {  
//					"^/api": ""  
//				}  
// 			}
// 		}
// 	}
// },
/*
 * 接口配置、请求方法、公共方法 
 * Auth:wuml
 */

// 开发环境
// var HOST = 'http://39.104.203.128:8083/app/'
// var HOST = 'http://39.104.203.128:8083/'
// var HOST = 'http://39.104.203.128:8084/'
// var HOST = 'http://192.168.101.60:8080/'

var HOST = ''

// #ifdef H5
HOST = '/api/'
// #endif
// #ifdef APP-PLUS
HOST = 'http://106.74.23.234:8083/' //"http://192.168.101.60:8085/"//
// HOST = 'http://192.168.101.211:8083/';
// #endif

var IMGPATH = 'http://39.104.203.128:8083/'

export const config = {
	HOST,
	IMGPATH,
	update: 'app/checkAppVersion',
	login: 'app/login',
	logout: 'app/getlogout',
	getBanner: 'app/getTitleNews',
	getNewsList: 'app/getNewsList',
	getNewsById: 'app/getNewsById',
	getDictByType: 'app/getDictByType',
	// 环境监测
	getMonitor: 'mp/em/info',
	// 标记点查询
	getMarkers: 'mp/em/tab',
	// 地区范围坐标
	getPolygons: 'mp/em/scope',
	// 标记点详情
	getMarkerInfo: 'mp/em',
	// 数字二道凹-监测详情
	getSzeda: 'mp/em/szeda',
	// 数字两河-监测详情
	getSzlh: 'mp/em/szlh',
	// 数字蛮汗山-监测详情
	getSzmhs: 'mp/em/szmhs',
	// 动态监测-下拉查询
	getSearchResult: 'mp/particulars',
	// 动态监测-曲线图表
	getCanvasData: 'mp/em/curve',
	// 修改密码
	editPassword: 'app/updatePwd',
	//新闻动态新区门户新区视频
	getNewAreaVideo: 'app/getPushNews',
	// 浏览新闻计算时间
	getNewTimeGe: 'app/userReadNum',
	// 我的-新闻排行-新闻热榜
	getMineHotNew: 'app/newsViewTotalPh',
	// 我的-新闻排行-阅读排行
	getMineReadRanking: 'app/viewNewsPh',
	// 生态指数-获取土壤地图点位数据
	getSoilMap: 'appShow/base/getSoilMap',
	// 生态指数-获取水质地图点位数据
	waterMap: "appShow/base/waterMap",
	// 生态指数-获取植被地图点位数据
	vegetationMap: "appShow/base/vegetationMap",
	// 生态指数-获取微生物地图数据
	microbialMap: "appShow/base/microbialMap",
	// 生态指数-获取土壤详细数据
	getSoilMessById: "appShow/base/getSoilMessById",
	// 生态指数-获取水质详细数据 
	waterMapById: "appShow/base/waterMapById",
	// 生态指数-获取植被详细数据
	vegetationBySampleId: "appShow/base/vegetationBySampleId",
	// 生态指数-获取微生物详细数据
	microbialDataById: "appShow/base/microbialDataById",
	// 蛮汗山-详细数据
	getMhsMessage: "appShow/base/getMhsMessage",
	// 数字两河-详细数据
	getszlhMessage: "appShow/base/getszlhMessage",
	// 二道凹-详细数据
	getedwMessage: "appShow/base/getedwMessage",
	// 环境指数
	getWeatherMess: "appShow/base/getWeatherMess",
	// 视频
	getRtspUrlByIndexCode: "appShow/rtsp/getRtspUrlByIndexCode",
	// 年份
	getWeatherYearAndMonth: "appShow/base/getWeatherYearAndMonth",
	// 点击
	findMessByEquimentId: "mp/em/findMessByEquimentId",
	// /appShow/base/zbScheme   植被  
	zbScheme: "appShow/base/zbScheme",
	// /appShow/base/trScheme    土壤
	trScheme: "appShow/base/trScheme",
}

export const $$ = {
	getVersion: function(callback) {
		plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
			console.log('getVersion', wgtinfo)
			$$.setStorage('version', wgtinfo.version, 1)
			return callback && callback(wgtinfo)
		})
	},
	getUpdate: function(isPrompt, callback) {
		let version
		$$.getVersion(v => {
			var data = {
				appid: plus.runtime.appid,
				versionCode: parseInt(v.versionCode),
				versionName: v.version,
				// imei: plus.device.imei
			}
			// var data = {
			// 	appid: "34432432243",
			// 	versionCode: 99,
			// 	versionName: "2343",
			// 	// imei: plus.device.imei
			// }
			$$.request({
				url: config.update,
				data: data,
				method: 'POST'
			}, isPrompt, false).then(res => {
				var data = res.data
				if (!$$.isEmptyObj(data) && $$.isNotNull(data.downloadUrl)) {
					$$.updateModal(data)
				} else {
					// 已是最新版
					if (isPrompt) {
						uni.showToast({
							title: '暂无新版本',
							icon: 'none'
						})
					}
				}
			}).catch(e => {
				console.log(e.errMsg)
			}).finally(() => {
				// console.log('complate')
			})
		})
	},
	updateModal: function(data) {
		uni.showModal({
			title: '提示',
			content: '发现新版本',
			showCancel: !data.forceUpdate,
			cancelText: '暂不更新',
			confirmText: '立即更新',
			success: res => {
				if (res.confirm) {
					var updateData = JSON.stringify(data)
					uni.reLaunch({
						url: '/pages/update/update?data=' + updateData
					})
				}
			},
			fail: () => {},
			complete: () => {}
		})
	},
	request: function(options, isLoading, isLogin) {
		if (isLoading) {
			// 显示加载
			uni.showLoading({
				title: ''
			})
		}

		var defaultOption = {
			url: config.HOST + options.url,
			data: options.data || {},
			method: options.method || 'GET',
			header: options.method === 'POST' ? {
				// 'cache-control': "no-cache, no-store, max-age=0, must-revalidate",
				'Content-Type': 'application/json;charset=utf-8',
			} : {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'x-www-form-urlencoded'
			},
			dataType: 'json',
		}
		if (isLogin) {
			var token = $$.getStorage('token')
			if (!token) {
				//去登录
				uni.reLaunch({
					url: '../login/login'
				})

			} else {
				defaultOption.header.Authorization = token
			}
		}
		return new Promise((reslove, reject) => {
			uni.request({
				url: defaultOption.url,
				data: defaultOption.data,
				method: defaultOption.method,
				header: defaultOption.header,
				dataType: 'json',
				success: res => {
					console.log(res);
					if (res.statusCode == 200) {
						//判断登录超时或是统一操作判断
						if (res.data.code == 2000 || res.data.code == 2001) {
							if (res.data.data && res.data.data.token) {
								$$.setStorage('token', res.data.data.token, 1)
							}
							reslove(res.data)
						}
						if (res.data.code == 401) {
							uni.removeStorageSync('userinfo')
							uni.removeStorageSync('token')
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
						if (res.data.code == 500) {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.errMsg
						})
						reject(res)
					}
				},
				fail: error => {
					console.log(defaultOption.url)
					console.log(defaultOption.data)
					console.log(defaultOption.method)
					console.log(defaultOption.header)
					console.log(error)
					console.log(error.errMsg)
					uni.showToast({
						icon: 'none',
						title: error.errMsg
					})
					reject(error)
				},
				complete: () => {
					uni.hideLoading()
				}
			})
		})
	},
	setStorage: function(key, val, expire) {
		let obj = {
			data: val,
			time: Date.now(),
			expire: expire * 24 * 60 * 60 * 1000 //天转换为毫秒
		}
		uni.setStorageSync(key, JSON.stringify(obj))
	},
	getStorage: function(key) {
		let val = uni.getStorageSync(key)
		if (!val) {
			return val
		}
		val = JSON.parse(val)
		// if (Date.now() - val.time > val.expire) {
		// 	uni.removeStorageSync(key);
		// 	return null;
		// }
		return val.data
	},
	isNotNull: function(obj) {
		if (
			!obj ||
			'null' === obj || {} === obj ||
			'{}' === obj ||
			'undefined' === obj ||
			'[object Object]' === obj || [] === obj ||
			'[]' === obj
		) {
			return false
		}
		return true
	},
	isEmptyObj: function(obj) {
		if ($$.isNotNull(obj)) {
			for (var key in obj) {
				return false
			}
		}
		return true
	}
}
